<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class InfoManage extends CI_Controller{
    /**
     * 学生信息添加模板
     * @return [type] [description]
     */
    public function student_add_plane(){
        session_start();
        $this->load->library('authority');
        $this->load->library('common');

        //base_url链接
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        if(!$this->authority->is_exist($_SESSION['user'])){
            $msg='请先登录!';
            redirect("admin/user/index?msg=$msg");
        }
        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();
        foreach ($academy_list as $key => $value) {
            $major_list[$value->co_id]=$this->college->getMajor($value->co_id);
        }
        echo $this->load->view('admin/student_add_plane',array('base'=>$base,"academy_list"=>$academy_list,"major_list"=>$major_list));
    }
    /**
     * 获取相关学院下的专业列表
     * @return [type] [description]
     */
    public function getMajor(){
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        $this->load->model("m_college","college",true);
        $result=$this->college->getMajor($_POST['id']);
        if($result){
            $success=true;
            $msg="成功！";
            echo json_encode(array('success'=>$success,"msg"=>$msg,"data"=>$result));
        }else{
            $success=false;
            $msg="失败！";
            echo json_encode(array('success'=>$success,"msg"=>$msg));
        }
    }
    /**
     * 添加学生信息
     * @return [type] [description]
     */
    public function student_add(){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");
        $this->load->model("m_student","student",true);

        $success=true;
        $str=array();
        $str['name']=@$_POST['name']?@$_POST['name']:'';
        $str['name']=mysql_real_escape_string($str['name']);

        if($str!='' && ($this->student->student_search($str))){
            $success=false;
            $msg="已经存在该学生！";
        }else{
            $str['sex']=@$_POST['sex']?@$_POST['sex']:'';
            $birth_year=@$_POST['birth-year']?@$_POST['birth-year']:'';
            $birth_month=@$_POST['birth-month']?@$_POST['birth-month']:'';
            if(!empty($birth_year) && !empty($birth_month)){
                $str['birth']=$birth_year.$birth_month;
            }
            $str['place']=@$_POST['place']?@$_POST['place']:'';
            $str['period']=@$_POST['period']?@$_POST['period']:'';
            $str['academy']=@$_POST['academy']?@$_POST['academy']:'';
            $str['major']=@$_POST['major']?@$_POST['major']:'';
            $str['class']=@$_POST['class']?@$_POST['class']:'';
            $str['ranking']=@$_POST['ranking']?@$_POST['ranking']:'';
            $str['ranking']=mysql_real_escape_string($str['ranking']);
            if(empty($str)){
                $success=false;
                $msg="请填写完整信息！";
            }else{
                foreach ($str as $key => $value) {
                    if($value==''){
                        $success=false;
                        $msg="请填写完整信息！";
                        break;
                    }
                }
            }
            if($success){
                //获取学生学号
                $this->load->model("m_college","college",true);
                $academy=$this->college->getNumber($str['academy']);
                $major=$this->college->getNumber($str['major']);
                $student_login["no"]=$str['period'].$academy[0]->number.$major[0]->number.$str['class'].$str['ranking'];

                //锁表
                $this->load->database();
                $this->db->query("LOCK TABLES stu_student WRITE;");
                $this->db->query("LOCK TABLES stu_s_login WRITE;");

                //开启事务
                $this->db->query("START TRANSACTION;");
                $this->db->query("SET AUTOCOMMIT=0;");
                try {
                    if($this->student->student_add($str)){
                        $str_name['name']=$str['name'];
                        $student_info=$this->student->student_search($str_name);
                        $student_login['s_id']=$student_info[0]->s_id;
                        if($this->student->student_login_add($student_login)){
                            $msg="添加成功！";
                            $this->db->query("COMMIT;");
                        }else{
                            $success=false;
                            $msg="添加失败！";
                        }
                    }else{
                        $success=false;
                        $msg="添加失败！";
                    }
                    if($success){
                        $this->db->query("UNLOCK TABLES;");
                    }else{
                        $this->db->query("ROLLBACK;");
                        $this->db->query("UNLOCK TABLES;");
                    }
                } catch (Exception $e) {
                    $msg = "系统出错，入库商品失败！请稍后再试！";
                    $sign = false;

                    // 事务回滚
                    $this->db->query("ROLLBACK;");

                    // 解锁
                    $this->db->query("UNLOCK TABLES;");
                }
            }
        }
        echo json_encode(array("base"=>$base,"success"=>$success,"msg"=>$msg));
    }
    /**
     * 学生查询模板
     * @return [type] [description]
     */
    public function student_search_plane(){
        session_start();
        $this->load->library('authority');
        $this->load->library('common');

        //base_url链接
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        if(!$this->authority->is_exist($_SESSION['user'])){
            $msg='请先登录!';
            redirect("admin/user/index?msg=$msg");
        }
        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();
        foreach ($academy_list as $key => $value) {
            $major_list[$value->co_id]=$this->college->getMajor($value->co_id);
        }
        echo $this->load->view('admin/student_search_plane',array('base'=>$base,"academy_list"=>$academy_list,"major_list"=>$major_list));
    }
    /**
     * 搜索学生信息
     * @param  string $page [description]
     * @return [type]       [description]
     */
    public function student_search($page="1-6.html"){
        session_start();
        $this->load->library('authority');
        $this->load->library('common');

        $pageNumber=$this->common->getPage($page);

        //base_url链接
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        $this->load->model("m_student","student",true);

        $msg="查询成功！";
        $success=true;
        $str=array();
        if(@$_POST['number']){$str['no']=@$_POST['number'];}

        if(@$_POST['name']){$name=@$_POST['name'];$str['name']=mysql_real_escape_string($name);}

        if(@$_POST['period']){ $str['period']=@$_POST['period'];}

        if(@$_POST['academy']){ $str['academy']=@$_POST['academy'];}

        if(@$_POST['major']){ $str['major']=@$_POST['major'];}

        if(@$_POST['class']){$str['class']=@$_POST['class'];}

        if(@$_POST['ranking']){ $ranking=@$_POST['ranking'];$str['ranking']=mysql_real_escape_string($ranking);}

        if(@$str['no']){
            $student_info=$this->student->student_login_search($str['no']);
            $panel=$this->load->view("admin/student_no_result",array("base"=>$base,"student_info"=>$student_info),true);
            echo json_encode(array("success"=>$success,"msg"=>$msg,"panel"=>$panel));
        }else if(@$str['period'] && @$str['academy'] && @$str['major']){
            $student_info=$this->student->student_search($str);
            $student_current_info=$this->common->pageInfo($pageNumber[0],$pageNumber[1],$student_info);
            $panel=$this->load->view("admin/student_some_result",array("base"=>$base,"pageInfo"=>$student_current_info),true);
            echo json_encode(array("success"=>$success,"msg"=>$msg,"panel"=>$panel));
        }else{
            $success=false;
            $msg="查询条件不足！";
            echo json_encode(array("success"=>$success,"msg"=>$msg));
        }
    }
    /**
     * 学生信息修改模板
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function student_update_plane($params){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();

        $str['s_id']=$params;

        //学生信息
        $this->load->model('m_student','student',true);
        $student_info=$this->student->student_search($str);

        //专业
        $array['number']=$student_info[0]->academy;
        $array['p_id']='0';
        $c_academy=$this->college->academy($array);

        $arr['p_id']=$c_academy[0]->co_id;
        $c_major=$this->college->academy($arr);

        echo $this->load->view('admin/student_search_change',array('base'=>$base,"academy_list"=>$academy_list,'student_info'=>$student_info,"c_major"=>$c_major));
    }
    /**
     * 修改学生信息
     * @return [type] [description]
     */
    public function student_info_change(){
        $success=true;
        $str1['s_id']=@$_POST['s_id']?@$_POST['s_id']:'';

        $str2['period']=@$_POST['period']?@$_POST['period']:'';
        $str2['academy']=@$_POST['academy']?@$_POST['academy']:'';
        $str2['major']=@$_POST['major']?@$_POST['major']:'';
        $str2['class']=@$_POST['class']?@$_POST['class']:'';
        $str2['ranking']=@$_POST['ranking']?@$_POST['ranking']:'';
        $str2['name']=@$_POST['name']?@$_POST['name']:'';
        $str2['sex']=@$_POST['sex']?@$_POST['sex']:'';
        $str2['birth']=@$_POST['birth']?@$_POST['birth']:'';
        $str2['place']=@$_POST['place']?@$_POST['place']:'';

        $str3['no']=$str2['period'].$str2['academy'].$str2['major'].$str2['class'].$str2['ranking'];

        $this->load->database();
        $this->db->query("LOCK TABLES stu_student WRITE;");
        $this->db->query("LOCK TABLES stu_s_login WRITE;");

        $this->db->query("START TRANSACTION;");
        $this->db->query("SET AUTOCOMMIT=0;");

        try {
            $this->load->model("m_student",'student',true);
            if($this->student->student_update($str2,$str1)){
                if($this->student->student_login_update($str3,$str1)){
                    $msg="修改成功！";
                    $this->db->query("COMMIT;");
                }else{
                    $success=false;
                    $msg="修改失败！";
                }
            }else{
                $success=false;
                $msg="修改失败！";
            }
            if($success){
                $this->db->query("UNLOCK TABLES;");
            }else{
                $this->db->query("ROLLBACK;");
                $this->db->query("UNLOCK TABLES;");
            }
        } catch (Exception $e) {
            $msg = "系统出错，入库商品失败！请稍后再试！";
            $sign = false;

            // 事务回滚
            $this->db->query("ROLLBACK;");

            // 解锁
            $this->db->query("UNLOCK TABLES;");
        }
        echo json_encode(array('msg'=>$msg));
    }
    /**
     * 教师信息添加
     * @return [type] [description]
     */
    public function teacher_add_panel(){
        session_start();

        //base_url链接
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();

        //课程列表
        $this->load->model("m_course","course",true);
        $course_list=$this->course->course_list();
        //选修/必修
        $style[0]='请选择';
        $style[1]='必修';
        $style[2]='选修';

        echo $this->load->view('admin/teacher_add_panel',array('base'=>$base,"academy_list"=>$academy_list,"course_list"=>$course_list,'style'=>$style));
    }
    /**
     * 添加老师信息
     * @return [type] [description]
     */
    public function teacher_add(){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");
        $this->load->model("m_teacher","teacher",true);

        $id='';
        $success=true;
        $str=array();
        $str['name']=@$_POST['name']?@$_POST['name']:'';
        $str['name']=mysql_real_escape_string($str['name']);

        $str['sex']=@$_POST['sex']?@$_POST['sex']:'';
        $str['academy']=@$_POST['academy']?@$_POST['academy']:'';

        $str['degree']=@$_POST['degree']?@$_POST['degree']:'';
        $str['degree']=mysql_real_escape_string($str['degree']);

        $str['c_id']=@$_POST['course']?@$_POST['course']:'';
        if(empty($str)){
            $success=false;
            $msg="请填写完整信息！";
        }else{
            foreach ($str as $key => $value) {
                if($value==''){
                    $success=false;
                    $msg="请填写完整信息！";
                    break;
                }
            }
        }
        if($success){

            $teacher_login["no"]=time();

            //锁表
            $this->load->database();
            $this->db->query("LOCK TABLES stu_teacher WRITE;");
            $this->db->query("LOCK TABLES stu_t_login WRITE;");

            //开启事务
            $this->db->query("START TRANSACTION;");
            $this->db->query("SET AUTOCOMMIT=0;");
            try {
                $id=$this->teacher->teacher_add($str);
                if($id){
                    $teacher_login['t_id']=$id;
                    if($this->teacher->teacher_login_add($teacher_login)){
                        $msg="添加成功！";
                        $this->db->query("COMMIT;");
                    }else{
                        $success=false;
                        $msg="添加失败！";
                    }
                }else{
                    $success=false;
                    $msg="添加失败！";
                }
                if($success){
                    $this->db->query("UNLOCK TABLES;");
                }else{
                    $this->db->query("ROLLBACK;");
                    $this->db->query("UNLOCK TABLES;");
                }
            } catch (Exception $e) {
                $msg = "系统出错，入库商品失败！请稍后再试！";
                $sign = false;

                // 事务回滚
                $this->db->query("ROLLBACK;");

                // 解锁
                $this->db->query("UNLOCK TABLES;");
            }
        }
        echo json_encode(array("base"=>$base,"success"=>$success,"msg"=>$msg));
    }
    /**
     * 教师查询模板
     * @return [type] [description]
     */
    public function teacher_search_panel(){
        session_start();
        $this->load->library('authority');
        $this->load->library('common');

        //base_url链接
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        if(!$this->authority->is_exist($_SESSION['user'])){
            $msg='请先登录!';
            redirect("admin/user/index?msg=$msg");
        }
        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();

        echo $this->load->view('admin/teacher_search_panel',array('base'=>$base,"academy_list"=>$academy_list));
    }
    /**
     * 搜索老师信息
     * @param  string $page [description]
     * @return [type]       [description]
     */
    public function teacher_search($page="1-6.html"){
        session_start();
        $this->load->library('authority');
        $this->load->library('common');

        $pageNumber=$this->common->getPage($page);

        //base_url链接
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        $this->load->model("m_teacher","teacher",true);

        $msg="查询成功！";
        $success=true;
        $str=array();
        if(@$_POST['number']){$str['no']=@$_POST['number'];}

        if(@$_POST['academy']){ $str['academy']=@$_POST['academy'];}

        if(!empty($str)){
            $teacher_info=$this->teacher->teacher_login_search($str);
            $teacher_current_info=$this->common->pageInfo($pageNumber[0],$pageNumber[1],$teacher_info);

            //学院
            $this->load->model("m_college",'college',true);
            $academy_list=$this->college->getAcademy();

            //课程
            $this->load->model("m_course",'course',true);
            $course_list=$this->course->course_list();

            $panel=$this->load->view("admin/teacher_search_result",array("base"=>$base,"pageInfo"=>$teacher_current_info,"academy_list"=>$academy_list,"course_list"=>$course_list),true);
            echo json_encode(array("success"=>$success,"msg"=>$msg,"panel"=>$panel));
        }else{
            $success=false;
            $msg="查询条件不足！";
            echo json_encode(array("success"=>$success,"msg"=>$msg));
        }
    }
}